/**
 * 高德地图对象
 */
var MapGaoDe = function (longitude, latitude, level) {
    /**
     * 当鼠标左键点击的时候触发这个事件
     */
    this.onMapMouseLeftClick = null;
    /**
     * 当鼠标右键点击的时候触发这个事件
     */
    this.onMapMouseRightClick = null;
    /**
     * 当鼠标按下的时候触发这个事件
     */
    this.onMapMouseDown = null;
    /**
     * 当鼠标移动的时候触发这个事件
     */
    this.onMapMouseMove = null;
    /**
     * 当鼠标抬起的时候触发这个事件
     */
    this.onMapMouseUp = null;
    /**
     * 当鼠标进入的时候触发这个事件
     */
    this.onMapMouseOver = null;
    /**
     * 当鼠标移出的时候触发这个事件
     */
    this.onMapMouseOut = null;

    var mapObj = this;

    this.map = new AMap.Map("iCenter", {
        center: new AMap.LngLat(longitude, latitude), // 地图中心点
        resizeEnable: true,
        level: level
        // 地图显示的缩放级别
    });

    //var hideAmapLogo = function () { // 隐藏高德地图的logo
    //    var $logo = $('#iCenter .amap-logo');
    //    if ($logo.length) {
    //        $('#iCenterMarker').hide();
    //        $('#iCenter').show();
    //        $logo.hide().next().hide();
    //    } else {
    //        setTimeout(hideAmapLogo, 500);
    //    }
    //};
    //setTimeout(hideAmapLogo, 500);

    AMap.event.addListener(this.map, 'click', eventMapMouseLeftClick);
    AMap.event.addListener(this.map, 'rightclick', eventMapMouseRightClick);
    AMap.event.addListener(this.map, 'mousedown', eventMapMouseDown);
    AMap.event.addListener(this.map, 'mousemove', eventMapMouseMove);
    AMap.event.addListener(this.map, 'mouseup', eventMapMouseUp);
    AMap.event.addListener(this.map, 'mouseover', eventMapMouseOver);
    AMap.event.addListener(this.map, 'mouseout', eventMapMouseOut);
    AMap.event.addListener(this.map, 'moving', eventMapMoving);

    function eventMapMouseLeftClick() {
        if (mapObj.onMapMouseLeftClick) {
            mapObj.onMapMouseLeftClick(mapObj);
        }
    }

    function eventMapMouseRightClick() {
        if (mapObj.onMapMouseRightClick) {
            mapObj.onMapMouseRightClick(mapObj);
        }
    }

    function eventMapMouseDown() {
        if (mapObj.onMapMouseDown) {
            mapObj.onMapMouseDown(mapObj);
        }
    }

    function eventMapMouseMove() {
        if (mapObj.onMapMouseMove) {
            mapObj.onMapMouseMove(mapObj);
        }
    }

    function eventMapMouseUp() {
        if (mapObj.onMapMouseUp) {
            mapObj.onMapMouseUp(mapObj);
        }
    }

    function eventMapMouseOver() {
        if (mapObj.onMapMouseOver) {
            mapObj.onMapMoumapObjver(mapObj);
        }
    }

    function eventMapMouseOut() {
        if (mapObj.onMapMouseOut) {
            mapObj.onMapMouseOut(mapObj);
        }
    }

    function eventMapMoving() {
        if (mapObj.onMapMoving) {
            mapObj.onMapMoving(mapObj);
        }
    }

    this.clear = function () {
        mapObj.map.clearMap();
        mapObj.map.destroy();
    };

    /**
     * 地图按像素偏移
     */
    this.mapOffset = function (x, y) {
        mapObj.map.panBy(x, y);
    };

    /**
     * 地图平移到目的点
     */
    this.mapMoveTo = function (longitude, latitude) {
        mapObj.map.panTo(new AMap.LngLat(longitude, latitude));
    };

    /**
     * 地图平移到目的点
     */
    this.mapMoveToCorrect = function (longitude, latitude) {
        mapObj.map.panTo(new AMap.LngLat(longitude, latitude));
    };

    this.clear = function () {
        mapObj.map.clearMap();
    };

    this.destroy = function () {
        try {
            mapObj.map.clearMap();
            mapObj.map.destroy();
        } catch (e) {
        }
    };

    /**
     * 地图自适应显示函数
     */
    this.mapFitView = function () {
        mapObj.map.setFitView();//使地图自适应显示到合适的范围
    };

    /**
     * 添加交通图
     */
    this.addTraffic = function () {
        // this.map.clearOverlays();
        trafficlay = new AMap.TileLayer(
            {
                tileSize: 256, // 图像大小
                zIndex: 5,
                id: "trafficlay",
                getTileUrl: function (x, y, z) {
                    return "http://tm.mapabc.com/trafficengine/mapabc/traffictile?v=1.0&t=1&zoom="
                        + (17 - z) + "&x=" + x + "&y=" + y;
                }
            });
        mapObj.map.addLayer(trafficlay);
    };

    /**
     * 移除交通图
     */
    function removeTraffic() {
        mapObj.map.removeLayer("trafficlay");
    }

    /**
     * 设置地图是否可鼠标拖拽
     */
    this.isDragEnable = function (tag) {
        mapObj.map.setStatus({
            dragEnable: tag
        });
    };

    /**
     * 设置地图是否可键盘移动
     */
    this.isKeyboardEnable = function (tag) {
        mapObj.map.setStatus({
            keyboardEnable: tag
        });
    };

    /**
     * 设置地图是否可双击放大
     */
    this.isDoubleClickZoom = function (tag) {
        mapObj.map.setStatus({
            doubleClickZoom: tag
        });
    };

    /**
     * 设置地图中心点和缩放级别
     */
    this.setMapCenterAndLevel = function (longitude, latitude, level) {
        mapObj.map
            .setZoomAndCenter(level, new AMap.LngLat(longitude, latitude));
    };

    /**
     * 设置地图中心城市
     */
    this.setMapCity = function (cityName) {
        mapObj.map.setCity(cityName);
    };

    this.setMapLevel = function (level) {
        mapObj.map.setZoom(level);
    };

    /**
     * 经纬度坐标转换为地理像素坐标
     */
    this.lngLat2ContainerPixel = function (longitude, latitude) {
        var pixel = mapObj.map.lnglatToPixel(new AMap.LngLat(longitude,
            latitude), mapObj.map.getZoom());
    };

    /**
     * 地理像素坐标转换为经纬度坐标
     */
    this.containerPixel2LngLat = function (x, y) {
        var ll = mapObj.map.pixelToLngLat(new AMap.Pixel(x, y), 10);
    };

    /**
     * 地图画线
     */
    this.drawLine = function (lnglatarr, color, width) {
        // 绘制轨迹
        var polyline = new AMap.Polyline({
            map: mapObj.map,
            path: lnglatarr,
            strokeColor: color,// 线颜色
            strokeOpacity: 1,// 线透明度
            strokeWeight: width,// 线宽
            strokeStyle: "solid"// 线样式
        });
    };

    /**
     * 地图画线
     */
    this.drawTrack = function (lnglatarr, color, width) {
        if (lnglatarr.length >= 2) {
            var green_flag_url = $.getProjectName() + "/component/map/images/green_flag.png";
            var red_flag_url = $.getProjectName() + "/component/map/images/red_flag.png";
            var greenFlagContent = document.createElement("div");
            greenFlagContent.className = "greenFlagContent";
            // 点标记中的图标
            var greenFlagMarkerImg = document.createElement("img");
            greenFlagMarkerImg.className = "greenFlagMarker";
            greenFlagMarkerImg.src = green_flag_url;
            greenFlagContent.appendChild(greenFlagMarkerImg);
            // 点标记中的文本
            var greenFlagMarkerLabel = document.createElement("span");
            greenFlagMarkerLabel.innerHTML = "起点";
            greenFlagContent.appendChild(greenFlagMarkerLabel);
            var greenFlagMarker = new AMap.Marker({
                map: mapObj.map,
                draggable: false, // 是否可拖动
                position: lnglatarr[0],// 基点位置
                offset: new AMap.Pixel(-3, -28), // 相对于基点的位置
                content: greenFlagContent,
                angle: 0,
                autoRotation: true
            });

            var redFlagContent = document.createElement("div");
            redFlagContent.className = "redFlagContent";
            // 点标记中的图标
            var redFlagMarkerImg = document.createElement("img");
            redFlagMarkerImg.className = "redFlagMarker";
            redFlagMarkerImg.src = red_flag_url;
            redFlagContent.appendChild(redFlagMarkerImg);
            // 点标记中的文本
            var redFlagMarkerLabel = document.createElement("span");
            redFlagMarkerLabel.innerHTML = "终点";
            redFlagContent.appendChild(redFlagMarkerLabel);
            var redFlagMarker = new AMap.Marker({
                map: mapObj.map,
                draggable: false, // 是否可拖动
                position: lnglatarr[lnglatarr.length - 1],// 基点位置
                offset: new AMap.Pixel(-3, -28), // 相对于基点的位置
                content: redFlagContent,
                angle: 0,
                autoRotation: true
            });
            greenFlagMarker.setMap(mapObj.map);
            redFlagMarker.setMap(mapObj.map);
        }
        // 绘制轨迹
        var polyline = new AMap.Polyline({
            map: mapObj.map,
            path: lnglatarr,
            strokeColor: color,// 线颜色
            strokeOpacity: 1,// 线透明度
            strokeWeight: width,// 线宽
            strokeStyle: "solid"// 线样式
        });
    };

    /**
     * 地图营运轨迹画线
     */
    this.drawRunTrack = function (lnglatarr) {
        if (lnglatarr.length >= 2) {
            var start_flag_url = $.getProjectName() + "/component/map/images/start_flag.png";
            var edd_flag_url = $.getProjectName() + "/component/map/images/end_flag.png";
            var greenFlagContent = document.createElement("div");
            greenFlagContent.className = "greenFlagContent";
            // 点标记中的图标
            var greenFlagMarkerImg = document.createElement("img");
            greenFlagMarkerImg.className = "greenFlagMarker";
            greenFlagMarkerImg.src = start_flag_url;
            greenFlagContent.appendChild(greenFlagMarkerImg);
            var greenFlagMarker = new AMap.Marker({
                map: mapObj.map,
                draggable: false, // 是否可拖动
                position: lnglatarr[0],// 基点位置
                offset: new AMap.Pixel(-11, -30), // 相对于基点的位置
                content: greenFlagContent,
                angle: 0,
                autoRotation: true
            });

            var redFlagContent = document.createElement("div");
            redFlagContent.className = "redFlagContent";
            // 点标记中的图标
            var redFlagMarkerImg = document.createElement("img");
            redFlagMarkerImg.className = "redFlagMarker";
            redFlagMarkerImg.src = edd_flag_url;
            redFlagContent.appendChild(redFlagMarkerImg);

            var redFlagMarker = new AMap.Marker({
                map: mapObj.map,
                draggable: false, // 是否可拖动
                position: lnglatarr[lnglatarr.length - 1],// 基点位置
                offset: new AMap.Pixel(-11, -30), // 相对于基点的位置
                content: redFlagContent,
                angle: 0,
                autoRotation: true
            });
            greenFlagMarker.setMap(mapObj.map);
            redFlagMarker.setMap(mapObj.map);
        }
        // 绘制轨迹
        var polyline = new AMap.Polyline({
            map: mapObj.map,
            path: lnglatarr,
            strokeColor: 332,// 线颜色
            strokeOpacity: 0.5,// 线透明度
            strokeWeight: 5,// 线宽
            strokeStyle: "solid"// 线样式
        });
    };

    /**
     * 创建一个消息窗口
     */
    this.createInfoWindow = function (title, content) {
        var info = [];
        info.push("<b>" + title + "</b>");
        info.push(content);
        var inforWindow = new AMap.InfoWindow({
            content: info.join("<br/>")
        });
        return inforWindow;
    };

    /**
     * 为地图添加插件，该插件用于显示切换地图类型
     */
    this.pluginMapType = function () {
        // 添加地图类型切换插件
        mapObj.map.plugin([ "AMap.MapType" ], function () {
            // 地图类型切换
            type = new AMap.MapType({
                defaultType: 0
            });// 初始状态使用2D地图
            mapObj.map.addControl(type);
        });
    };

    /**
     * 加载比例尺插件
     */
    this.pluginScale = function () {
        mapObj.map.plugin([ "AMap.Scale" ], function () {
            scale = new AMap.Scale();
            mapObj.map.addControl(scale);
        });
    };

    /**
     * 在地图中添加ToolBar插件
     */
    this.pluginToolBar = function () {
        mapObj.map.plugin([ "AMap.ToolBar" ], function () {
            toolBar = new AMap.ToolBar();
            mapObj.map.addControl(toolBar);
        });
    };

    /**
     * 在地图中添加鹰眼插件
     */
    this.pluginOverView = function () {
        mapObj.map.plugin([ "AMap.OverView" ], function () {
            // 加载鹰眼
            overView = new AMap.OverView();
            mapObj.map.addControl(overView);
            overView.open();
        });
    };

    /**
     * 在地图中添加鼠标工具插件
     */
    this.pluginMouseTool = function () {
        mapObj.map.plugin([ "AMap.MouseTool" ], function () {
            // 鼠标工具插件
            mousetool = new AMap.MouseTool(mapObj.map);
            mapObj.map.addControl(mousetool);
        });
    };
};

/**
 * 地图公交对象
 */
var Bus = function (mapObj, busNo, longitude, latitude, direction, info,upDn) {
    /**
     * 当鼠标左键点击的时候触发这个事件
     */
    this.onBusMouseLeftClick = null;
    /**
     * 当鼠标右键点击的时候触发这个事件
     */
    this.onBusMouseRightClick = null;
    /**
     * 当鼠标按下的时候触发这个事件
     */
    this.onBusMouseDown = null;
    /**
     * 当鼠标移动的时候触发这个事件
     */
    this.onBusMouseMove = null;
    /**
     * 当鼠标抬起的时候触发这个事件
     */
    this.onBusMouseUp = null;
    /**
     * 当鼠标进入的时候触发这个事件
     */
    this.onBusMouseOver = null;
    /**
     * 当鼠标移出的时候触发这个事件
     */
    this.onBusMouseOut = null;
    /**
     * 当车辆移动中
     */
    this.onBusMoving = null;
    this.busNo = busNo;
    this.longitude = longitude;
    this.latitude = latitude;
    this.direction = direction;
    var infoWindow; // 鼠标悬浮消息框
    var bus = this;
    var infoMsg = info;
    var busOnlineUrl;
    if(upDn==0){
        busOnlineUrl = $.getProjectName() + "/component/map/images/car_online.png";
    }else{
        busOnlineUrl = $.getProjectName() + "/content/images/car_online2.png";
    }
   // var busOnlineUrl = $.getProjectName() + "/component/map/images/car_online.png";

    var busNoMarkerContent = document.createElement("div");
    busNoMarkerContent.setAttribute("style", "font-style:\"黑体\";font-weight:bold;color:red;width:70px;");
    busNoMarkerContent.setAttribute("title", busNo);
    busNoMarkerContent.innerHTML = busNo;
    /**
     * 车辆标记
     */
    this.busMarker = new AMap.Marker({
        map: mapObj.map,
        draggable: false, // 是否可拖动
        position: new AMap.LngLat(this.longitude, this.latitude),// 基点位置
        //offset : new AMap.Pixel(-10, -8), // 相对于基点的位置
        //content : busMarkerContent,
        offset: new AMap.Pixel(-12, -9),
        icon: busOnlineUrl,
        angle: parseFloat(this.direction) - 90,
        autoRotation: true
    });
    this.busNoMarker = new AMap.Marker({
        map: mapObj.map,
        draggable: false, // 是否可拖动
        position: new AMap.LngLat(this.longitude, this.latitude),// 基点位置
        offset: new AMap.Pixel(-35, 12), // 相对于基点的位置
        content: busNoMarkerContent,
        autoRotation: true
    });
    AMap.event.addListener(this.busMarker, 'click', eventBusMouseLeftClick);
    AMap.event.addListener(this.busMarker, 'rightclick', eventBusMouseRightClick);
    AMap.event.addListener(this.busMarker, 'mousedown', eventBusMouseDown);
    AMap.event.addListener(this.busMarker, 'mousemove', eventBusMouseMove);
    AMap.event.addListener(this.busMarker, 'mouseup', eventBusMouseUp);
    AMap.event.addListener(this.busMarker, 'mouseover', eventBusMouseOver);
    AMap.event.addListener(this.busMarker, 'mouseout', eventBusMouseOut);
    AMap.event.addListener(this.busMarker, 'moving', eventBusMoving);

    /**
     * 设置小车的方向
     */
    this.setDirection = function (directionParam) {
        this.direction = directionParam;
        this.busMarker.setRotation(parseFloat(this.direction) - 90);
    };

    /**
     * 显示公交
     */
    this.show = function () {
        this.busMarker.setMap(mapObj.map);
        this.busNoMarker.setMap(mapObj.map);
    };
    /**
     * 清除公交
     */
    this.clear = function () {
        this.busMarker.setMap(null);
        this.busNoMarker.setMap(null);
    };
    this.clear(); // 默认不显示

    function eventBusMouseLeftClick() {
        if (bus.onBusMouseLeftClick) {
            bus.onBusMouseLeftClick(bus);
        }
    }

    function eventBusMouseRightClick() {
        if (bus.onBusMouseRightClick) {
            bus.onBusMouseRightClick(bus);
        }
    }

    function eventBusMouseDown() {
        if (bus.onBusMouseDown) {
            bus.onBusMouseDown(bus);
        }
    }

    function eventBusMouseMove() {
        if (bus.onBusMouseMove) {
            bus.onBusMouseMove(bus);
        }
    }

    function eventBusMouseUp() {
        if (bus.onBusMouseUp) {
            bus.onBusMouseUp(bus);
        }
    }

    function eventBusMouseOver() {
        if (infoMsg && infoMsg != "") {
            infoWindow = mapObj.createInfoWindow("信息", infoMsg);
            infoWindow.open(mapObj.map, new AMap.LngLat(bus.longitude,
                bus.latitude));
        }
        if (bus.onBusMouseOver) {
            bus.onBusMouseOver(bus);
        }
    }

    function eventBusMouseOut() {
        if (infoWindow) {
            infoWindow.close();
        }
        if (bus.onBusMouseOut) {
            bus.onBusMouseOut(bus);
        }
    }

    function eventBusMoving() {
        bus.longitude = bus.busMarker.getPosition().getLng();
        bus.latitude = bus.busMarker.getPosition().getLat();
        bus.busNoMarker.setPosition(bus.busMarker.getPosition());
        if (infoWindow) {
            infoWindow.setPosition(new AMap.LngLat(bus.longitude, bus.latitude));
        }
        if (bus.onBusMoving) {
            bus.onBusMoving(bus);
        }
    }
};